﻿using System;
using System.Collections.Generic;
using NoMiddleman.Core.Domain;

namespace NoMiddleman.Facades.DataTransferObjects
{
    public class ClientDTO
    {
        public int Id { get; set; }

        public string Login { get; set; }

        public string Password { get; set; }

        public AddressDTO Address { get; set; }

        public DateTime DateRegistered { get; set; }

        public IList<AdvertDTO> Adverts { get; set; }

        public bool IsIndividual { get; set; }

        public string Name { get; set; }

        public string Regon { get; set; }

        public string FirstName { get; set; }

        public string SurName { get; set; }
    }

    public class ClientFactory
    {
        public static Client GetClient(ClientDTO clientDto)
        {
            if(clientDto.IsIndividual)
            {
                Individual entity = new Individual {FirstName = clientDto.FirstName, SurName = clientDto.SurName};
                FillClient(entity, clientDto);
                return entity;
            }else
            {
                RealEstateAgency entity = new RealEstateAgency {Regon = clientDto.Regon, Name = clientDto.Name};
                FillClient(entity, clientDto);
                return entity;
            }
        }

        private static void FillClient(Client entity, ClientDTO clientDTO)
        {
            entity.DateRegistered = clientDTO.DateRegistered;
            entity.Login = clientDTO.Login;
            entity.Password = clientDTO.Password;
        }

        public static ClientDTO GetClientDTO(Client client)
        {
            ClientDTO clientDTO = new ClientDTO
                                      {
                                          Id = client.Id,
                                          DateRegistered = client.DateRegistered,
                                          Login = client.Login,
                                          Password = client.Password
                                      };
            clientDTO.Address = AddressFactory.GetAddressDTO(client.Address);
            if(client is Individual)
            {
                var c = client as Individual;
                clientDTO.FirstName = c.FirstName;
                clientDTO.SurName = c.SurName;
            }else
            {
                var c = client as RealEstateAgency;
                clientDTO.Regon = c.Regon;
                clientDTO.Name = c.Name;
            }
            return clientDTO;
        }
    }
}